﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;
using System.Security.Cryptography;
using System.Xml;
using System.Text;

public partial class Manager_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtUserName.Focus();
            //UpdateRate();
        }

    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        clsCommon clsCom = new clsCommon();

        RijndaelManaged mobjCryptoService = new RijndaelManaged();
        EncrypKey clsEncryp = new EncrypKey(mobjCryptoService);
        clsWebShop cls = new clsWebShop();
        string UserNm = txtUserName.Text.Trim();
        string Password = clsEncryp.Encrypting(cls.killSQLInjectionLogin(txtPassword.Text), System.Configuration.ConfigurationManager.AppSettings["EncrypKey"]);


        DataSet ds = new DataSet();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("UserNm", cls.killSQLInjectionLogin(UserNm));
        msg.CreateFirstNode("Password", Password);
        msg.CreateFirstNode("SuperAdmin", System.Configuration.ConfigurationManager.AppSettings["SuperAdmin"]);

        ds = cls.Get_InfLoginCondition(msg.GetXmlStr.ToString());
        DataTable dtExist = ds.Tables["TExist"];
        if (dtExist.Rows.Count > 0)
        {
            if (dtExist.Rows[0]["Exist"].ToString() == "Y")
            {
                DataTable dtUser = new DataTable();
                DataTable dtMenu = new DataTable();
                dtUser = ds.Tables[0];
                dtMenu = ds.Tables[1];
                Session["AdminUserID"] = dtUser.Rows[0]["UserID"].ToString();
                Session["UserName"] = dtUser.Rows[0]["UserNm"].ToString();
                Session["CusName"] = dtUser.Rows[0]["CusNm"].ToString();
                Session["UserEmail"] = dtUser.Rows[0]["UserID"].ToString();
                Session["AutID"] = dtUser.Rows[0]["AutID"].ToString();
                Session["AuthTable"] = dtMenu;
                Session["IsAdmin"] = "Y";
                Response.Redirect("MainMenu.aspx");

            }
            else if (dtExist.Rows[0]["Exist"].ToString() == "S")
            {
                DataTable dtUser = new DataTable();
                DataTable dtMenu = new DataTable();
                dtUser = ds.Tables[0];
                dtMenu = ds.Tables[1];
                Session["AdminUserID"] = dtUser.Rows[0]["UserID"].ToString();
                Session["UserName"] = dtUser.Rows[0]["UserNm"].ToString();
                Session["CusName"] = dtUser.Rows[0]["CusNm"].ToString();
                Session["UserEmail"] = System.Configuration.ConfigurationManager.AppSettings["SuperEmail"];
                Session["AutID"] = dtUser.Rows[0]["AutID"].ToString();
                Session["AuthTable"] = dtMenu;
                Session["IsAdmin"] = "S";
                //Response.Redirect("ManagerHome.aspx");
                Response.Redirect("MainMenu.aspx");

            }
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Đăng nhập không thành công')</script>");

        }
    }

    private void UpdateRate()
    {
        try
        {
            PccMsg msg = new PccMsg();
            msg.CreateFirstNode("RateID", "0");
            msg.CreateFirstNode("RateDate", "");
            clsWebShop cls = new clsWebShop();

            DataTable dt = null;
            dt = cls.Get_RateByCondition(msg.GetXmlStr).Tables[0];

            string UrlBank = System.Configuration.ConfigurationManager.AppSettings["URLBank"];
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(UrlBank);
            string ExrateList = xmlDoc.SelectSingleNode("ExrateList/DateTime").InnerText;
            if (ExrateList != "")
            {
                string[] Date = ExrateList.Split('/');
                string Day = Date[1];
                string Month = Date[0];
                string Year = Date[2].Substring(0, 4);
                string[] Time = Date[2].ToUpper().Replace("PM", "").Replace("AM", "").Split(':');
                string Hour = Time[0].Split(' ')[1];
                string Minute = Time[1];
                string Second = Time[2];
                DateTime dtBank = new DateTime(int.Parse(Year), int.Parse(Month), int.Parse(Day), int.Parse(Hour), int.Parse(Minute), int.Parse(Second));
                string sBMoney = "";
                foreach (XmlNode node in xmlDoc.SelectNodes("ExrateList/Exrate"))
                {
                    if (node.Attributes.GetNamedItem("CurrencyCode").Value.ToUpper() == "USD")
                    {
                        sBMoney = node.Attributes.GetNamedItem("Sell").Value;
                    }
                }
                if (dt.Rows.Count > 0)
                {
                    string CountDayGetRate = System.Configuration.ConfigurationManager.AppSettings["CountDayGetRate"];
                    string sDate = dt.Rows[0]["RateDate"].ToString();
                    DateTime dtDb = new DateTime(int.Parse(sDate.Substring(0, 4)), int.Parse(sDate.Substring(4, 2)), int.Parse(sDate.Substring(6, 2)), int.Parse(sDate.Substring(8, 2)), int.Parse(sDate.Substring(10, 2)), int.Parse(sDate.Substring(12, 2)));
                    dtDb = dtDb.AddDays(int.Parse(CountDayGetRate));

                    if (dtDb.CompareTo(dtBank) < 0)
                    {
                        string sBankNm = xmlDoc.SelectSingleNode("ExrateList/Source").InnerText;
                        string sBDate = dtBank.ToString("yyyyMMddhhmmss");
                        msg = new PccMsg();
                        msg.CreateFirstNode("RateId", "0");
                        msg.CreateFirstNode("RateDate", sBDate);
                        msg.CreateFirstNode("EditMK", "0");
                        msg.CreateFirstNode("BankNm", sBankNm);
                        msg.CreateFirstNode("USD", sBMoney);
                        msg.CreateFirstNode("Status", "N");
                        msg.CreateFirstNode("UpdID", "-2");
                        cls.Ins_Upd_Rate(msg.GetXmlStr);
                    }
                }
                else if (dt.Rows.Count == 0)
                {
                    string sBankNm = xmlDoc.SelectSingleNode("ExrateList/Source").InnerText;
                    string sBDate = dtBank.ToString("yyyyMMddhhmmss");
                    msg = new PccMsg();
                    msg.CreateFirstNode("RateId", "0");
                    msg.CreateFirstNode("RateDate", sBDate);
                    msg.CreateFirstNode("EditMK", "0");
                    msg.CreateFirstNode("BankNm", sBankNm);
                    msg.CreateFirstNode("USD", sBMoney);
                    msg.CreateFirstNode("Status", "N");
                    msg.CreateFirstNode("UpdID", "-2");
                    cls.Ins_Upd_Rate(msg.GetXmlStr);
                }
            }
        }
        catch { }
    }
    private string RandomString(int size)
    {
        StringBuilder builder = new StringBuilder();
        Random random = new Random();
        char ch;
        for (int i = 0; i < size; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            builder.Append(ch);
        }

        return builder.ToString();
    }


}
